/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Hinhanh;
import save.OpenFileDialog;
import until.HibernateUtil;

/**
 *
 * @author Admin
 */
public class hinhAnhDAO {

    public static List<Hinhanh> LayHinhAnh() {
        List<Hinhanh> ds = null;
        Session sc = HibernateUtil.getSessionFactory().openSession();

        try {

            String sql = "From Hinhanh";
            Query query = sc.createQuery(sql);
            ds = query.list();

        } catch (Exception e) {
            System.err.println(e);
        } finally {
            sc.close();
        }
        return ds;

    }

    public static void themHinhAnh(Hinhanh hinhanh) {
        Session sc = HibernateUtil.getSessionFactory().openSession();
        
        Transaction transaction = null;
        try {
            transaction = sc.beginTransaction();
            sc.save(hinhanh);
            transaction.commit();
        } catch (Exception e) {
            System.err.println(e);
            transaction.rollback();
           
        } finally {
            sc.close();
        }
        
    }

    public static boolean updateHinhAnh(Hinhanh hinhanh) {
        Session sc = HibernateUtil.getSessionFactory().openSession();
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = sc.beginTransaction();
            sc.update(hinhanh);
            transaction.commit();

        } catch (Exception e) {
            System.err.println(e);
            transaction.rollback();
            kq = false;
        } finally {
            transaction.rollback();
        }
        return kq;
    }

    public static String layHinhAnh() {
        OpenFileDialog file = new OpenFileDialog();
        file.getFilePath();
        file.isFileSelected();
        return file.getFileName();
    }
}
